<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>掃碼報工及異常停機</title>
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">	
		<link rel="stylesheet" href="./4/lumen/bootstrap.css" media="screen">
		<link rel="stylesheet" href="./_assets/css/custom.min.css">
	</head>
	<body>
		<div class="container">
			<div class="form-group row">
				<h2>工序:</h2>
				<div class="col-sm-1">
					<input class="form-control" readonly="readonly" id="Working_pro" type="text">
				</div>
			</div>	
			<div class="row">
				<label class="col-sm-2 col-form-label" for="inputDefault">請依序掃二維碼:</label>
				<input class="form-control" placeholder="AN1811074-X 35M5 4" id="box" type="text">
			</div>
			<div class="row">
				<div id="barcode_data">目前工序: 機台: 操機員: 日期: 工號: 數量:</div>
			</div>
			<div class="row" id="Work_situation">
				<div class="card border-primary mb-3" style="max-width: 20rem;">
					<div class="card-header" id ="a1">機台號:BPX3</div>
					<div class="card-body">
						<p class="card-title" id="b1">工单号</p>
						<p class="card-text" id="c1">产能:盒</p>
						<p class="card-text" id="c1">片</p>
						<p class="card-text" id="d1">结束<p>
					</div>
				</div>	
			</div>
			<hr class="my-4">
			<div class="row">
				<div class="card border-primary mb-3" style="max-width: 20rem;">
					<div class="card-header">機台號:BPX0099</div>
					<div class="card-body">
						<h4 class="card-title">異常:</h4>
						<p class="card-text">開始時間:08:23</p>
						<p class="card-text">結束時間:</p>
					</div>
				</div>		
				<div class="card border-primary mb-3" style="max-width: 20rem;">
					<div class="card-header">機台號:BPX0099</div>
					<div class="card-body">
						<h4 class="card-title">異常:</h4>
						<p class="card-text">開始時間:18:23</p>
						<p class="card-text">結束時間:</p>
					</div>
				</div>		
			</div>
		</div>
		<script src="./_vendor/jquery/dist/jquery.min.js"></script>
		<script src="./_vendor/popper.js/dist/umd/popper.min.js"></script>
		<script src="./_vendor/bootstrap/dist/js/bootstrap.min.js"></script>
		<script src="./_assets/js/custom.js"></script>
		<script>
			var Product_Number=0;//用于ID动态地址的递增
			var k=0;//用来判定是否是第一次扫描
			var f="no";//用于判定是否已有机台，若没有就自动添加机台
			var forder="null";//判断工单号是否一样，一样就在当前机台下创建《工单号，产能等》
			var cho;//锁定相同机台号的ID
			var sorder=-1;//锁定相同工单号ID
			var capa = new Array();//盒
			var slices = new Array();//片
			var database = new Array();//存入数据库的数据
			var working_pro = new Array();//存放所有工序
			var datacata=0;//记录扫到一次END内的盒数
			var dataslices=0;//记录扫到一次END内的片数
			var jitaihao;//机台号
			var time = null;//先定义时间变量
			var YC = {
				 "EXC22":"日点检保养"
				,"EXC23":"工单切换调机"
				,"EXC24":"更换耗材"
				,"EXC25":"冶具清理"
				,"EXC26":"首件"
				,"EXC27":"生产调机"
				,"EXC11":"设备故障调机"
				,"EXC31":"其他计划外停机"
			}
			database[2]=0;//存入数据库的数据
			database[3]=0;
			working_pro[0]="BPJ";//存放工序
			working_pro[1]="BPA";
			working_pro[2]="BPD";
			working_pro[3]="BPI";
			working_pro[4]="BPC";
			$("#box").keypress(function(e){
				key = e.which;
				if(key==13){//按下回车之后的操作
				//var time=new Date();
				//var times= time.toLocaleString( );
				//alert(times);
					var QR_code=$("#box").val();//收集回来的信息
					FrontThreeString=(QR_code.substring(0,3));//前三位
					FrontTwoString=(QR_code.substring(0,2));//前两位
					//for(){}
				
					for(var i=0;i<working_pro.length;i++){//循环判断各个工序
						if(FrontThreeString=="BPJ")//如果前三码等于工序的编码则执行操作
							$("#Working_pro").val("编带");
						if(FrontThreeString=="BPA")
							$("#Working_pro").val("固晶");
						if(FrontThreeString=="BPD")
							$("#Working_pro").val("点胶");
						if(FrontThreeString=="BPI")
							$("#Working_pro").val("分光");
						if(FrontThreeString=="BPC")
							$("#Working_pro").val("焊线");
						
					}
					if(FrontTwoString=="BP"){//取出前两个字符判断是不是与机台前缀匹配
						database[0]=QR_code;//如果开头前2码为BP就判断它是机台号,然后执行操作
						console.log(QR_code);
						for(var i=0;i<Product_Number;i++){//循环判断动态地址的递增值
							console.log($("#Machine"+i).text());
							//alert("機台號:"+QR_code+"--"+$("#Machine"+i).text());
							if("機台號:"+QR_code==$("#Machine"+i).text()){
								f="yes";
								jitaihao="機台號:"+QR_code;
								//alert(f);
								cho=i;//记录与那个机台号相同
								//alert(f+"--"+k);
							}
						}
					}
					if(FrontTwoString=="TA"&&f=="yes"){//取出前两个字符判断是不是与工单号前缀匹配并且机台匹配成功
							//alert("工单号:"+QR_code+"--"+$("#Work_order"+cho).text());
							for(var i=0;i<Product_Number;i++){//循环判断动态地址的递增值
								if("工单号:"+QR_code==$("#Work_order"+i).text()&&$("#Machine"+i).text()==jitaihao){//工单号相同且机台号相同就直接标记为yes
									forder="yes";
									sorder=i;//记录相同工单号ID方便后续扫BOX与PCS定位
									//cho=i;//记录与那个机台号相同
									//alert(f+"--"+k);
								}
							}
							if(sorder==-1){
								forder="no";
								//alert(forder);
							}
					}
					if(f=="no"){//机台编号不一样执行
						if(FrontTwoString=="BP"){
							capa[Product_Number]=0;
							slices[Product_Number]=0;
							var  Machine = $("<div class='card-header' id='"+'Machine'+Product_Number+"'></div>").append("機台號:"+QR_code);
							var  Work_order = $("<p id='"+'Work_order'+Product_Number+"'></p>").append("工单号:");
							var  Capacity = $("<p id='"+'Capacity'+Product_Number+"'></p>").append("产能:盒");
							var  Slice = $("<p id='"+'Slice'+Product_Number+"'></p>").append("片:");
							var  ENDs = $("<p id='"+'ENDs'+Product_Number+"'></p>").append("");
							var ss=$("<div  class='card-body' id='"+'ss'+Product_Number+"'></div>").append(Work_order).append(Capacity).append(Slice).append(ENDs);
							//$("<tr id='"+'shipmenttr'+disksnumber+"'></tr>");
							$("<div class='card border-primary mb-3' style='max-width: 20rem;' id='"+'CODE'+Product_Number+"'> </div>")
								.append(Machine).append(ss)
								.appendTo("#Work_situation");
						}
						if(FrontTwoString=="TA"){
						database[1]=QR_code;
						var ww=$("#Work_order"+Product_Number).text("工单号:"+QR_code);
						}
						if(FrontThreeString=="BOX"){
							capa[Product_Number]=capa[Product_Number]+1;
							$("#Capacity"+Product_Number).text("产能:"+capa[Product_Number]+"盒");
							database[2]=capa[Product_Number];
							//alert(capa[Product_Number]);
						}
						if(FrontThreeString=="PCS"){
							slices[Product_Number]=slices[Product_Number]+1;
							$("#Slice"+Product_Number).text("片:"+slices[Product_Number]+"片");
							database[3]=slices[Product_Number];
							//alert(capa[Product_Number]);
						}
						if(FrontThreeString=="END"){
							$("#ENDs"+Product_Number).text("结束");
							Product_Number++;
							f="no";
							forder="null";
							sorder=-1;
							/*时间*/
							time = GetDateStr(0);
							alert(database[0]+"--"+database[1]+"--"+database[2]+"--"+database[3]+"--"+time);
							$.ajax({
								type:'POST',
								url:"ScanCode.php",
								data:"&Machine="+database[0]+"&Work_order="+database[1]+"&Capacity="+database[2]+"&Slice="+database[3]+"&Time="+time,
								dataType : "text",
								success: function(data){
									//$("#msg").val(data).css('color','green');
									alert(data);
									
								}
							});
							datacata=0;
							dataslices=0;
							database[2]=0;
							database[3]=0;
						}
					}
					if(f=="yes"&&forder=="no"){//机台编号一样且工单号不一样，直接在相同机台号下延伸
						if(FrontTwoString=="TA"){
							capa[Product_Number]=0;
							slices[Product_Number]=0;
							//alert("TA is no ");
							var  Machine = $("<div class='card-header' id='"+'Machine'+Product_Number+"'></div>").append("機台號:");
							var  Work_order = $("<p id='"+'Work_order'+Product_Number+"'></p>").append("工单号:");
							var  Capacity = $("<p id='"+'Capacity'+Product_Number+"'></p>").append("产能:盒");
							var  Slice = $("<p id='"+'Slice'+Product_Number+"'></p>").append("片:");
							var  ENDs = $("<p id='"+'ENDs'+Product_Number+"'></p>").append("");
							$("<div class='card-body' id='"+'ss'+Product_Number+"'> </div>")
												.append(Machine).append(Work_order).append(Capacity).append(Slice).append(ENDs)
												.appendTo("#CODE"+cho);
							//$("<tr id='"+'shipmenttr'+disksnumber+"'></tr>");
						}
						$("#Machine"+Product_Number).text(jitaihao);
						if(FrontTwoString=="TA"){
							database[1]=QR_code;
							var ww=$("#Work_order"+Product_Number).text("工单号:"+QR_code);
						}
						if(FrontThreeString=="BOX"){
							capa[Product_Number]=capa[Product_Number]+1;
							$("#Capacity"+Product_Number).text("产能:"+capa[Product_Number]+"盒");
							database[2]=capa[Product_Number];
							//alert(capa[Product_Number]);
						}
						if(FrontThreeString=="PCS"){
							slices[Product_Number]=slices[Product_Number]+1;
							$("#Slice"+Product_Number).text("片:"+slices[Product_Number]+"片");
							database[3]=slices[Product_Number];
							//alert(capa[Product_Number]);
						}
						if(FrontThreeString=="END"){
							$("#ENDs"+Product_Number).text("结束");
							sorder=-1;
							f="no";
							forder="null";
							/*时间*/
							time = GetDateStr(0);
							alert(database[0]+"--"+database[1]+"--"+database[2]+"--"+database[3]+"--"+time);
							$.ajax({
								type:'POST',
								url:"ScanCode.php",
								data:"&Machine="+database[0]+"&Work_order="+database[1]+"&Capacity="+database[2]+"&Slice="+database[3]+"&Time="+time,
								dataType : "text",
								success: function(data){
									//$("#msg").val(data).css('color','green');
									alert(data);
								}
							});
							datacata=0;
							dataslices=0;
							database[2]=0;
							database[3]=0;
							Product_Number++;
						}
					}
					if(f=="yes"&&forder=="yes"){//机台号一样并且工单号一样直接在原有基础上+box和pcs
						/*if(FrontTwoString=="TA"){
						database[1]=QR_code;
						var ww=$("#Work_order"+cho).text("工单号:"+QR_code);
						}*/
						if(FrontThreeString=="BOX"){
							//alert(capa[cho]);
							SumCapacity= parseInt(1)+ parseInt(capa[sorder]);
							$("#Capacity"+sorder).text("产能:"+SumCapacity+"盒");
							capa[sorder]=SumCapacity;//总产量
							//database[2]=SumCapacity-parseInt(capa[cho]);
							datacata++;//此次扫码做的产量
							database[2]=datacata;
						}
						if(FrontThreeString=="PCS"){
							//alert(capa[cho]);
							SumSlice= parseInt(1)+ parseInt(slices[sorder]);
							$("#Slice"+sorder).text("片:"+SumSlice+"片");
							slices[sorder]=SumSlice;//总产量
							dataslices++;//此次扫码做的产量
							database[3]=dataslices;
						}
						if(FrontThreeString=="END"){
							$("#ENDs"+sorder).text("结束");
							sorder=-1;
							f="no";
							forder="null";
							time = GetDateStr(0);
							alert(database[0]+"--"+database[1]+"--"+database[2]+"--"+database[3]+"--"+time);
							$.ajax({
								type:'POST',
								url:"ScanCode.php",
								data:"&Machine="+database[0]+"&Work_order="+database[1]+"&Capacity="+database[2]+"&Slice="+database[3]+"&Time="+time,
								dataType : "text",
								success: function(data){
									//$("#msg").val(data).css('color','green');
									alert(data);
								}
							});
							datacata=0;
							dataslices=0;
							database[2]=0;
							database[3]=0;
						}
					}
					//if(k==3)(
						//$("#ENDs"+Product_Number).text("结束");
						//k=-1;
						//Product_Number++;
					//)
					//alert($("#Machine"+Product_Number).text());
					$("#box").val("");
				}
			});
			//计算某天之后的日期,-1为昨天,0为今天,1为明天
			function GetDateStr(AddDayCount) {
				var dd = new Date();
				dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
				var y = dd.getFullYear();
				var m = dd.getMonth()+1;//获取当前月份的日期
				var d = dd.getDate();
				var hh = dd.getHours();  //获取当前小时数(0-23)
				var min = dd.getMinutes();
				var ss = dd.getSeconds();
				return y + "-" + m + "-" + d+" " + hh + ":" + min + ":" + ss;
			}			
		</script>
	</body>
</html>